package open.witype.filetiper.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import open.witype.filetiper.R;

/**
 * Created by Typer on 2015/5/26 0026.
 * 数据库操作的Helper类
 */
public class DBHelper extends SQLiteOpenHelper {

    // 这个是DB文件名字
    public static final String DATABASE_FILENAME = "remarks_data.db";

    public static final int DATABASE_VERSION = 2;

    // 这个是自己项目包路径
    public static final String PACKAGE_NAME = "open.witype.filetiper";

    // 获取存储位置地址
    public static final String DATABASE_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME + "/databases/";

    public static final String REMARK_TABLE = "remarks";

    public static final String ID = "id";

    public static final String PATH = "path";

    public static final String REMARK = "remark";

    public static final String VERSION = "version";

    public static final String FAVO_TABLE = "favo";

    public static final String FAVO_PATH = "favo_path";

    public static final String FAVO_REMARK   = "favo_remark";

    public static final String FAVO_INDEX = "favo_index";

    public static final String FAVO_SHOW = "favo_show";

    public static final String FAVO_ENUM = "favo_enum";

    public DBHelper(Context context) {
        super(context, DATABASE_FILENAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        File databaseFile = new File(DATABASE_PATH + DATABASE_FILENAME);
        if (databaseFile.exists())
            return;
        String sql = "CREATE TABLE " + REMARK_TABLE + " ("
                + ID + " INTEGER primary key autoincrement, "
                + PATH + " text(256) not null, "
                + REMARK + " text(256) not null,"
                + VERSION + " INTEGER DEFAULT 1);";
        db.execSQL(sql);
        sql = "CREATE TABLE "+FAVO_TABLE+" ("
                + ID +  " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + FAVO_PATH + " TEXT(256) NOT NULL,"
                + FAVO_REMARK + " TEXT(256),"
                + FAVO_INDEX + " INTEGER NOT NULL,"
                + FAVO_SHOW + " INTEGER DEFAULT 1,"
                + FAVO_ENUM + " TEXT DEFAULT DIR);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + REMARK_TABLE;
        db.execSQL(sql);
        sql = "DROP TABLE IF EXISTS " + FAVO_TABLE;
        db.execSQL(sql);
        onCreate(db);
    }

    /**
     * 移动res/raw文件夹下的数据库文件到默认的数据库文件为位置
     *
     * @param context Context
     */
    public void copyDatabaseFile(Context context) {
        try {
            String databaseFilename = DATABASE_PATH + DATABASE_FILENAME;
            File dir = new File(DATABASE_PATH);
            if (!dir.exists()) {
                dir.mkdir();
            }
            if (!(new File(databaseFilename)).exists()) {
                InputStream is = context.getResources().openRawResource(R.raw.remarks_data);
                FileOutputStream fos = new FileOutputStream(databaseFilename);
                byte[] buffer = new byte[8192];
                int count;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
